import Mock from "mockjs";
const userList = [];
const count = 3;


//扩展mockjs生成手机号
Mock.Random.extend({
    phone: function () {
      var phonePrefixs = ['135', '189', '159', '132']
      return this.pick(phonePrefixs) + Mock.mock(/\d{8}/)
    }
})

function getQueryVariable(url, name) {
    let index = url.indexOf('?');
    if(index != -1) {
        let queryParams = url.substring(index+1).split('&');
        for(let i = 0; i< queryParams.length; i++) {
            var params = queryParams[i].split("=");
            if(params[0] == name) {
                return params[1];
            }
        }
    }else {
        return null;
    }
}

for (let i = 0; i < count; i++) {
    userList.push( Mock.mock(
        {
            id: '@increment(100)',
            username: '@name()',
            password: '@string()',
            email: '@email()',
            phone: '@phone()', 
            createTime: '@datetime("yyyy-MM-dd HH:mm:ss")'
        }
    ))
}
Mock.mock(/\/user\/getById/, 'get', (options) => {
    const id = getQueryVariable(options.url, "id");
    var res = null;
    // userList.forEach(e => {
        
    // })
    res = userList[id];
    return {
        code: 200,
        msg: "数据获取成功",
        data: {
            user: res
        }
    }
})
export default Mock;